#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "Parser.h"

//Splits a word with size greater than 5 characters into small blocks
void slipt(char **words, unsigned int *total, char *word, int lengh)
{
	int j = 0;
	char aux[MAXWORD + 1];
	
	for(int i = 0; i < lengh; i++)
	{
		if(j == MAXWORD)
		{
			aux[j] = '\0';
			strcpy(words[*total], aux);
			(*total)++;
			aux[0] = word[i];
			j = 1;
		}
		else
		{
			aux[j] = word[i];
			j++;
		}
	}

	aux[j] = '\0';
	strcpy(words[*total], aux);
	(*total)++;
}


//Parsing the block and adding the words on a vector
void parse(char **words, unsigned int *total, char *block, int size)
{

	char word[MAXWORD + 45];
	unsigned int i, j, lengh;

	j = 0;

	for(i = 0; i < size; i++){
		if((block[i] >= 97) && (block[i] <= 123)){
			word[j] = block[i];
			j++;
		}
		else{
			word[j] = '\0';
			j = 0;
			lengh = strlen(word);
			if(lengh > 1){
				if(lengh <= MAXWORD)
				{
					
					strcpy(words[*total], word);
					(*total)++;
				}
				else
					slipt(words, total, word, lengh);
			}
        
		}
	}
}

void wordsAdjustment(unsigned int *newWord)
{
	*newWord = *newWord*FACTOR + 1;
}